<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/28 0028
 * Time: 15:27
 */
namespace app\index\controller;
use app\admin\model\SettingModel;
use app\admin\model\UserModel;
use app\index\model\Api;
use app\index\model\UpFile;
use think\Request;

class Update
{
    /**更新打卡设置
     * @param Request $request
     * @return \think\response\Json
     */
    public function setting(Request $request){
        $data = $request->param();
        //检测是否登录
        if(!$user = isSign()){
            return returns(401,'请登录');
        }
        //检测权限
        $quanxian = Api::AuthIndex('部门打卡设置');
        if(!$quanxian){
            return returns(403,'你的权限不足');
        }
        //return $data;
        //处理数据
        $array = explode(':',$data['work_shift']);
        $data['work_shift'] = $array[0]*3600+$array[1]*60;
        $array = explode(':',$data['closing_time']);
        $data['closing_time'] = $array[0]*3600+$array[1]*60;
        $data['attendance'] = explode(',',trim($data['attendance'],','));
        $data['wifi_ip'] = ip();
        $data['late_time'] = $data['late_time']*60;
        $data['overtime']  = $data['overtime']*60;
        $data['company_id'] = $user['company_id'];
        $data['department_id'] = $user['department_id'];
        $data['update_time'] = time();
        $setting = SettingModel::getSetting()->where('card_setting.department_id',$user['department_id'])->find();
        if($setting){
            $data['id'] = $setting['id'];
            if(SettingModel::updateSetting($data)){
                return returns(200,'更新打卡设置成功');
            }else{
                return returns(407,'更新打卡设置失败');
            }
        }else{
            if(SettingModel::addSetting($data)){
                return returns(200,'更新打卡设置成功');
            }else{
                return returns(407,'更新打卡设置失败');
            }
        }
    }

    /**修改前台用户头像
     * @param Request $request
     * @return array
     */
    public function userPortrait(Request $request){
        if(!$user = isSign()){
            return returns(401);
        }
        //保存上传图片
        $filename = UpFile::getFile($request);
        if($filename){
            $filename = $filename['file'];
        }else{
            return returns(403,'请选择图片');
        }

        //更新自己的头像
        $data['id'] = $user['id'];
        $data['portrait'] = $filename;
        if(UserModel::updateIndexUser($data)){
            $user['portrait'] = $filename;
            session('user',$user);
            return returns(200,'头像修改成功');
        }else{
            return returns(403,'头像修改失败');
        }
    }
}